package com.platform.modules.wallet.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.mail.MailUtil;
import com.platform.common.constant.AppConstants;
import com.platform.common.enums.YesOrNoEnum;
import com.platform.common.utils.redis.RedisUtils;
import com.platform.common.web.service.impl.BaseServiceImpl;
import com.platform.modules.chat.service.ChatUserService;
import com.platform.modules.wallet.dao.WalletDao;
import com.platform.modules.wallet.domain.Wallet;
import com.platform.modules.wallet.enums.IdentityStatusEnum;
import com.platform.modules.wallet.enums.OpenNoEnum;
import com.platform.modules.wallet.enums.WalletStatusEnum;
import com.platform.modules.wallet.service.WalletAccountService;
import com.platform.modules.wallet.service.WalletAlarmService;
import com.platform.modules.wallet.service.WalletDailyBillhService;
import com.platform.modules.wallet.service.WalletService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service("walletService")
public class WalletServiceImpl extends BaseServiceImpl<Wallet> implements WalletService {


    @Autowired
    private RedisUtils redisUtils;
    @Resource
    private WalletDao walletDao;
    @Resource
    private WalletDailyBillhService dailyBillhService;
    @Resource
    private WalletAccountService accountService;
    @Resource
    private WalletAlarmService alarmService;

    @Autowired
    public void setBaseDao() {
        super.setBaseDao(walletDao);
    }


    @Override
    public Wallet createWallet(Long userId) {

        Wallet wallet = getBean();
        wallet.setUserId(userId);
        this.add(wallet);
        return wallet;
    }

    @Override
    public Wallet getWallet(Long userId) {

        Wallet wallet = getById(userId);
        if (wallet == null) {
            wallet = createWallet(userId);
        }
        return wallet;
    }

    @Override
    public int getBalance(Long userId) {

        int today = 0;
        int bill = 0;
        boolean isUpdate = false;

        String todayKey = AppConstants.WALLET_ACCOUNT_BALANCE + userId;
        if (redisUtils.hasKey(todayKey)) {
            String today_value = redisUtils.get(todayKey);
            if (StrUtil.isNotEmpty(today_value)) {
                today = Integer.parseInt(today_value);
            } else {
                today = accountService.queryTodaySum(userId).intValue();
                redisUtils.set(todayKey, String.valueOf(today), 10, TimeUnit.SECONDS);
                isUpdate = true;
            }
        } else {
            today = accountService.queryTodaySum(userId).intValue();
            redisUtils.set(todayKey, String.valueOf(today), 10, TimeUnit.SECONDS);
            isUpdate = true;
        }

        String billKey = AppConstants.WALLET_BILL_BALANCE + userId;
        String bill_value = redisUtils.get(billKey);
        if (StrUtil.isEmpty(bill_value)) {
            bill = dailyBillhService.querySum(userId).intValue();
            redisUtils.set(billKey, String.valueOf(bill), 60, TimeUnit.SECONDS);
        } else {
            bill = Integer.parseInt(bill_value);
        }

        int all = bill + today;

        if (isUpdate) {
            Wallet wallet = getWallet(userId);
            wallet.setBalance(all);
            update(wallet);
        }
        if (all < 0) {
            String contentTemp = "账号余额有误,userId：{}";
            String format = StrUtil.format(contentTemp, userId);
            alarmService.addAlarm(format);
//            MailUtil.send("61990988@qq.com", "系统发出告警,账单Bill加密校验失败", format, false);
        }

        return all;
    }

    @Override
    public int getBalanceNow(Long userId) {

        int today = accountService.queryTodaySum(userId).intValue();
        int bill = dailyBillhService.querySum(userId).intValue();

        String todayKey = AppConstants.WALLET_ACCOUNT_BALANCE + userId;
        redisUtils.set(todayKey, String.valueOf(today), 10, TimeUnit.SECONDS);


        String billKey = AppConstants.WALLET_BILL_BALANCE + userId;
        redisUtils.set(billKey, String.valueOf(bill), 60, TimeUnit.SECONDS);

        int all = bill + today;

        Wallet wallet = getWallet(userId);
        wallet.setBalance(all);
        update(wallet);

        return all;
    }

//    @Override
//    public synchronized void add(Integer amount, Long userId) {
//        Wallet wallet = getById(userId);
//        wallet.setAmount(wallet.getAmount() + amount.intValue());
//        wallet.setUpdateTime(DateUtil.date());
//        updateById(wallet);
//    }
//
//    @Override
//    public synchronized void subtract(Integer amount, Long userId) {
//        Wallet wallet = getById(userId);
//        wallet.setAmount(wallet.getAmount() - amount.intValue());
//        wallet.setUpdateTime(DateUtil.date());
//        updateById(wallet);
//    }

    @Override
    public void update(Wallet wallet) {
        updateById(wallet);
    }

    private Wallet getBean() {

        Wallet wallet = new Wallet();
//        wallet.setAmount(0);
//        wallet.setBalance(0);
        wallet.setStatus(WalletStatusEnum.NORMAL);
        wallet.setCreateTime(DateUtil.date());
        wallet.setIdentityStatus(IdentityStatusEnum.NO);
//        wallet.setRisk(0);
        wallet.setOpenRisk(YesOrNoEnum.NO);
        wallet.setOpenNo(OpenNoEnum.NORMAL);
        return wallet;
    }
}
